var app = getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowtime:null,
    next: true,
    list: [],
    optinos: [],
    redId: null,
    setting: {},
    authModle: false,
    memberId: null,
    page: 0,
    size: 10000,
    modleShow: 0,
    detailUrl: "/api/memberShop/goodsDetail.do",
    submitUrl: "/api/balance/sendRed.do",
    action: "/api/balance/productlist.do",
    submit: "{{submit}}"
  },
  toDetailssS: function (event) {
    var dataset = event.currentTarget.dataset;
    var shopid = dataset.shopid;
    var parmas = { id: shopid }
    ux.request(this.data.detailUrl, parmas, function (protocol) {
      console.log(JSON.stringify(protocol.data));
      wx.navigateTo({
        url: '/pages/member/tab1/shop/detail?id=' + protocol.data.entity.id + "&prices=" + protocol.data.entity.price,
      })
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    this.setData({
      nowtime:new Date().getTime(),
      userInfo: wx.getStorageSync("userInfo"),
      merchant: wx.getStorageSync("banrdId")
    })
    this.getList();
  },
  getList: function () {
    var self = this;
    if (self.data.next == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = { banrdId: self.data.merchant, pageNo: parseInt(self.data.page), pageSize: self.data.size };
    ux.request(self.data.action, params, function (data) {
      console.log(JSON.stringify(data))
      var datass = data.data.list;
      var datas=[];
      for (var i = 0; i < datass.length;i++){
        if (datass[i].number>0){
          datas.push(datass[i]);
          }
      }
      if (data.data.pageInfo.next == true) {
        self.setData({
          next: true
        });
      } else {
        self.setData({
          next: false
        });
      }
      if (self.data.page == 0) {
        self.setData({
          list: datas
        });
      } else {
        self.setData({
          list: self.data.list.concat(datas)
        });
      }
      var pages = self.data.page + 1;
      self.setData({
        page: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });
  },
  onForm: function (event) {
    if (this.data.submit) {
      var $this = this;
      var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit, detail.value, function (protocol) {
        if (protocol.success) {
          if (dataset.url) {
            wx.redirectTo(dataset.url);
          }
        }
      }, true);
    }
  },
  jianCount: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    if (this.data.list[index].count == 0) {
      return false
    } else {
      var lists = this.data.list;
      lists[index].count = lists[index].count - 1;
      lists[index].number = lists[index].number + 1;
      this.setData({
        list: lists
      })
    }
  },
  preventTouchMoveTy: function () {
    return;
  },
  jiaCount: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    if (this.data.list[dataset.index].number==0) {
      return false
    } else {
      var lists = this.data.list;
      lists[index].count = lists[index].count + 1;
      lists[index].number = lists[index].number - 1;
      for (var i = 0; i < lists.length; i++) {
        if (index != i) {
          lists[i].number = lists[i].number + lists[i].count;
          lists[i].count = 0;
        }
      }
      this.setData({
        list: lists
      })
    }
  },
  onTap: function (event) {
    console.log("JSON.stringify(event)");
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val) {
      data[dataset.key] = dataset.val;
    }
    this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var self = this;
      self.setData({
        authModle: false
      })
      wx.showShareMenu({
        withShareTicket: true
      })
      var shareObj = {
        title: self.data.setting.redTitle,
        path: '/pages/member/tab1/red/redGet?merchant=' + self.data.merchant + '&memberId=' + self.data.userInfo.uid + "&redId=" + self.data.redId,
        imageUrl: self.data.setting.redImg,
        success: function (res) {
          console.log(JSON.stringify(res));
        },
        fail: function () {
          wx.showToast({
            title: '转发失败',
          })
        }
      }
      return shareObj;
    }else{
     
    }
  },
  tijiao: function (event) {
    var dataset = event.currentTarget.dataset;
    var type = dataset.type;
    var lists = this.data.list;
    var tFlag = false;
    var thisId=0; var count=0;
    var oldIndex=null;
    for (var i = 0; i < lists.length; i++) {
      if (lists[i].count > 0) {
        thisId = lists[i].id;
        count = lists[i].count;
        oldIndex=i;
        tFlag = true;
      }
    }
    if (!tFlag) {
      wx.showToast({
        title: '请选择红包数量',
      })
      return false;
    }
    var self=this;
    var params = { id: parseInt(thisId), count: parseInt(count), type: parseInt(type)};
    ux.request(self.data.submitUrl, params, function (data) {

      var redId = data.data.redId;
      var setting = data.data.setting;
      var memberId = data.data.memberId;
      lists[oldIndex].count =0;
        self.setData({
          list: lists,
          redId: redId,
          setting: setting,
          memberId: memberId,
          authModle:true
        })
    });

  }
})
